package com.qdaily.ui.ui.fragment;

import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.supportlibrary.util.QDJsonUtil;
import com.qdaily.ui.MainActivity;
import com.qdaily.ui.QDBaseFragment;
import com.qdaily.ui.R;
import com.qdaily.ui.controller.ActivityController;
import com.qdaily.ui.net.NetConfigs;
import com.qdaily.ui.net.entity.ArticleListEntity;
import com.qdaily.ui.net.model.ArticleMeta;
import com.qdaily.ui.ui.adapter.ArticleListAdapter;
import com.qdaily.ui.ui.views.OnRefreshAndLoadMoreListener;
import com.qdaily.ui.ui.views.QDRefreshListView;

/**
 * 我的研究院。
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/7/15
 * Time: 14:52
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class MainSecondLabelFragment extends QDBaseFragment implements
        OnRefreshAndLoadMoreListener
        , AdapterView.OnItemClickListener {
    private QDRefreshListView qdRefreshListView;
    private ArticleListEntity articleListEntity;
    private ArticleListAdapter adapter;
    private boolean isHasMore;
    private boolean isLoading;
    private boolean isLastListEmpty;

    @Override
    protected int getLayoutId() {
        return R.layout.view_pullrefresh_listview;
    }

    @Override
    protected void setupViews(View view) {
        qdRefreshListView = (QDRefreshListView) view.findViewById(R.id.qdRefreshListView);
        qdRefreshListView.setOnRefreshAndLoadMoreListener(this);
        qdRefreshListView.setOnItemClick(this);
        adapter = new ArticleListAdapter(getContext());
        qdRefreshListView.setAdapter(adapter);
        if (getActivity() instanceof MainActivity) {
            final MainActivity activity = (MainActivity) getActivity();
            qdRefreshListView.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    activity.onChildScrollStateChange(view, scrollState);
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                }
            });
            qdRefreshListView.setOnTouchListener(new View.OnTouchListener() {
                private float y;
                private int scrollFlagY = 10;

                @Override
                public boolean onTouch(View var1, MotionEvent event) {
                    int my = 0;
                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN:
                            y = event.getY();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            if (y == 0) {
                                y = event.getY();
                            }
                            my = (int) (event.getY() - y);
                            if (my < -scrollFlagY) {
                                activity.onChildScroll(false);
                            } else if (my > scrollFlagY) {
                                activity.onChildScroll(true);
                            }
                            break;
                        default:
                            y = 0;
                            break;
                    }
                    return false;
                }
            });
        }
    }

    @Override
    protected void setupData() {

    }

    @Override
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
        super.onRequestCallBack(params, response);
        if (params instanceof NetConfigs.RequestQuestionnaireLists) {
            isLoading = false;
            boolean isFirst = false;
            NetConfigs.RequestQuestionnaireLists requestQuestionnaireLists = (NetConfigs.RequestQuestionnaireLists) params;
            isFirst = requestQuestionnaireLists.getmLastTime() == 0;

            articleListEntity = QDJsonUtil.Json2Object(response.toString(), ArticleListEntity.class);
            isHasMore = articleListEntity.getResponse().getFeeds().isHas_more();
            //文章列表
            if (isFirst) { //说明请求的第一页
                isLastListEmpty = adapter.addTopData(articleListEntity.getResponse().getFeeds().getList());
            } else {
                isLastListEmpty = adapter.addBottomData(articleListEntity.getResponse().getFeeds().getList());
            }
            qdRefreshListView.onRefreshComplete();
        }
    }

    @Override
    protected void onRequestErrorCallBack(NetUtil.NetParams params, Object response) {
        isLoading = false;
        qdRefreshListView.onRefreshComplete();
        qdRefreshListView.hasMoreLoadData(true, true);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        ArticleMeta articleMeta = (ArticleMeta) parent.getAdapter().getItem(position);
        displayArticleDetails(articleMeta);
    }

    private void displayArticleDetails(ArticleMeta articleMeta) {
        ActivityController.DisplayDetailActivity(getContext(), articleMeta);
    }

    @Override
    public void onRefreshBegin() {
        requestQuestionList(0);
    }

    private void requestQuestionList(long lastTime) {
        if (isLoading)
            return;
        isLoading = true;
        getRequestManager().requestRequestQuestionnaireLists(lastTime);
    }

    @Override
    public void onLoadMore() {
        if (isHasMore) {
            requestQuestionList(articleListEntity.getResponse().getFeeds().getLast_time());
        }
    }

    @Override
    public void onUIRefreshComplete() {
        qdRefreshListView.hasMoreLoadData(isLastListEmpty, isHasMore);
    }
}
